<?php

class Article extends Zend_Db_Table{
  protected $_name = 'article';
  protected $_primary = 'id';

  public function getArticleById($artId){
      $where = "id=$artId";
      $res = $this->fetchRow($where);
      return $res;
  }
  public function getPreArticle($posttime, $cateId){
      $where = "posttime>$posttime and cateid=$cateId";
      $order = "posttime";
      $arts = $this->fetchAll($where,$order)->toArray();
      if(isset($arts[0])) return $arts[0];
      return null;
  }
  public function getNextArticle($posttime, $cateId){
      $where = "posttime<$posttime and cateid=$cateId";
      $order = "posttime desc";
      $arts = $this->fetchAll($where,$order)->toArray();
      if(isset($arts[0])) return $arts[0];
      return null;
  }

  public function getArticlesByCate($cateId){
      $where = "cateid=".$cateId;
      $order = "posttime desc";
      return  $this->fetchAll($where,$order)->toArray();
  }

  public function getArticlesWithFirst($cateId){
      $arts =  $this->getArticlesByCate($cateId);
      $artsInCate = array(
          'first' => null,
          'rest' => array(),
      );
      if(isset($arts[0])){
          $artsInCate['first'] = $arts[0];
          unset($arts[0]);
          $artsInCate['rest'] = $arts;
      }
      return $artsInCate;
  }

  public function getArticlesByKeyword($keyword){
      $keyword = str_replace("'","‘",$keyword);
      //$keyword = str_replace('"',"“",$keyword);
      $where = "title like '%".$keyword."%'";
      $order = "posttime";
      $res = $this->fetchAll($where,$order);
      return $res;
  }

  public function getRecommendArticles(){
      $order = "viewnum desc";
      $count = 6;
      $offset = 0;
      return  $this->fetchAll(null,$order,$count,$offset);
  }

}
